//
//  ComposeController.m
//  CMJWeibo
//
//  Created by BH on 15/12/4.
//
//

#import "ComposeController.h"
#import "PlaceholderTextView.h"
#import "ComposePhotosView.h"
#import "ComposeToolBar.h"
#import "UIView+MJ.h"
#import "SendStatusParam.h"
#import "StatusTool.h"
#import <MBProgressHUD/MBProgressHUD.h>

@interface ComposeController ()<UITextViewDelegate,ComposeToolBarDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    MBProgressHUD *hud;
    NSString *placeholder;
}

@property(nonatomic,strong)PlaceholderTextView *textView;
@property(nonatomic,strong)ComposePhotosView *photosView;
@property(nonatomic,strong)ComposeToolBar *toolBar;

@end

@implementation ComposeController
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self.textView becomeFirstResponder];
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupNavBar];

    //添加输入控件
    [self setupInputView];
    
    //添加工具条
    [self setupToolBar];
}

/** 设置Controller 发微博 or 发评论 */
-(void)setType:(ComposeType)type
{
    if (type == ComposeTypeBlog) {
        self.title = @"发微博";
        placeholder = @"分享新鲜事。。。";
    }else{
        self.title = @"发评论";
        placeholder = @"写评论。。。";
    }
}

/**
 *  设置导航栏
 */
-(void)setupNavBar
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(send:)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
}

/**
 *  取消
 */
-(void)cancel:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 *  发微博
 */
-(void)send:(id)sender
{
    if (self.photosView.images.count) {
        //有图片
        [self sendWithImage];
    }else{
        //没有图片
        [self sendWithoutImage];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 *  发送带图片的微博
 */
-(void)sendWithImage
{}

/**
 *  发送不带图片的微博
 */
-(void)sendWithoutImage
{
    SendStatusParam *param = [SendStatusParam param];
    param.status_content = self.textView.text;
    
    [StatusTool sendStatusWithParam:param success:^(SendStatusResult *result) {
        hud = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
        hud.labelText = @"发送成功";
        [self.navigationController.view addSubview:hud];
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

/**
 *  添加输入控件
 */
-(void)setupInputView
{
    //添加输入框
    PlaceholderTextView *textV = [[PlaceholderTextView alloc] init];
    textV.delegate = self;
    textV.font = [UIFont systemFontOfSize:15];
    textV.placeholder = placeholder;
    textV.frame = CGRectMake(0, 0, self.view.width, self.view.height);
    [self.view addSubview:textV];
    self.textView = textV;
    
    //监听输入框文字改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:textV];
    
    //添加相册
    ComposePhotosView *photosV = [[ComposePhotosView alloc] init];
    photosV.y = 100;
    photosV.width = textV.width;
    photosV.height = textV.height;
    [self.view addSubview:photosV];
    self.photosView = photosV;
}

//监听文字改变
-(void)textChange
{
    self.navigationItem.rightBarButtonItem.enabled = (self.textView.text.length != 0);
}

/**
 *  添加工具条
 */
-(void)setupToolBar
{
    ComposeToolBar *toolB = [[ComposeToolBar alloc] init];
    toolB.delegate = self;
    toolB.width = self.view.width;
    toolB.height = 35;
    toolB.y = self.view.height-toolB.height;
    toolB.frame = CGRectMake(0, toolB.y, toolB.width, toolB.height);
    [self.view addSubview:toolB];
    self.toolBar = toolB;
    
    //监听键盘的弹出和隐藏
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

/**
 *  键盘弹出
 */
-(void)keyBoardWillShow:(NSNotification *)notif
{
    // 键盘弹出需要的时间
    CGFloat duration = [notif.userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    [UIView animateWithDuration:duration animations:^{
        //取出键盘高度
        CGRect keyBoardF = [notif.userInfo [UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat keyBoardH = keyBoardF.size.height;
        self.toolBar.transform = CGAffineTransformMakeTranslation(0, -keyBoardH);
    }];
}

/**
 *  键盘隐藏
 */
-(void)keyBoardWillHide:(NSNotification *)notif
{
    // 键盘弹出需要的时间
    CGFloat duration = [notif.userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    [UIView animateWithDuration:duration animations:^{
        self.toolBar.transform = CGAffineTransformIdentity;
    }];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 *  打开相册
 */
-(void)openAlbum
{
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    ipc.delegate = self;
    [self presentViewController:ipc animated:YES completion:nil];
}

/**
 *  打开照相机
 */
-(void)openCamera
{
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
    ipc.delegate = self;
    [self presentViewController:ipc animated:YES completion:nil];
}

#pragma mark - UIImagePickerController delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //销毁选图片的控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    //获取选中的图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    
    //添加到相册View中
    [self.photosView addImage:image];
}

#pragma mark - ComposeToolBar delegate
-(void)composetoolbar:(ComposeToolBar *)toolbar didClickButtonType:(ComposeToolBarButtonType)type
{
    switch (type) {
        case ComposeToolBarButtonTypeCamera:
            [self openCamera];
            break;
        case ComposeToolBarButtonTypePicture:
            [self openAlbum];
            break;
            
        default:
            break;
    }
}

#pragma mark - UITextView delegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.textView endEditing:YES];
}

@end
